using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;
using System.IO;

public partial class miniweb_GoldMemberProfile : System.Web.UI.Page
{
    protected long Id
    {
        get
        {
            object o = ViewState["Id"];
            if (o != null)
                return (long)o;
            return long.MinValue;
        }
        set
        {
            ViewState["Id"] = value;
        }
    }
    protected string nameCompany = string.Empty;
    protected string emailCompany = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlAnchor aMenuGold = (HtmlAnchor)Page.Master.FindControl("aMenuGold");
        aMenuGold.Attributes.Add("class", "Select");
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            this.Id = long.Parse(Request.QueryString["id"]);
        if (IsPostBack)
            return;
        LoadData();
    }
    #region Load Data Company
    private void LoadData()
    {



        if (Session["CompanyID"] != null)
        {
            this.Id = long.Parse(Session["CompanyID"].ToString());
            Company _Company = CompanyService.GetCompany(this.Id);
            if (_Company != null)
            {
                Session["MemberID"] = _Company.MemberID;
                Session["CompanyID"] = _Company.CompanyID;
                nameCompany = _Company.CompanyName;
                emailCompany = _Company.Email;

                //Basic Information
                ltrCompanyName.Text = _Company.CompanyName;
                ltrCountryAdd.Text = _Company.Address;
                ltrnameRes.Text = _Company.RepresentativeName;
                ltryearEs.Text = _Company.YearEstablishment;
                ltrRepresentative.Text = _Company.RegisteredCapital;
                string where = "where MajorMarketID in (Select CompanyAndMajor.MajorID from CompanyAndMajor where CompanyID=" + _Company.CompanyID + ")";
                MajorMarketCollection _MajorMarketCollection = MajorMarketService.GetMajorMarketListByParam("[" + MajorMarket._Table + "].[" + MajorMarketColumns.SortOrder + "]", OrderDirection.ASC, where);
                if (_MajorMarketCollection.Count > 0)
                {
                    int i = _MajorMarketCollection.Count;
                    int j = 0;
                    string Major = string.Empty;
                    foreach (MajorMarket _MajorMarket in _MajorMarketCollection)
                    {
                        j = j + 1;
                        if (j != i)
                            Major += _MajorMarket.Title + " , ";
                        else
                            Major += _MajorMarket.Title;
                    }
                    ltrMarket.Text = Major;

                }
                //
                string whereType = "where TypeCompanyID in (Select CompanyAndTypeCom.TypeComID from CompanyAndTypeCom where CompanyID=" + _Company.CompanyID + ")";
                TypeCompanyCollection _TypeCompanyCollection = TypeCompanyService.GetTypeCompanyListByParam("[" + TypeCompany._Table + "].[" + TypeCompanyColumns.SortOrder + "]", OrderDirection.ASC, whereType);
                if (_TypeCompanyCollection.Count > 0)
                {
                    int i = _TypeCompanyCollection.Count;
                    int j = 0;
                    string Type = string.Empty;
                    foreach (TypeCompany _TypeCompany in _TypeCompanyCollection)
                    {
                        j = j + 1;
                        if (j != i)
                            Type += _TypeCompany.Title + " , ";
                        else
                            Type += _TypeCompany.Title;
                    }
                    ltrTypecompany.Text = Type;

                }
                //
                ltrExportturnover.Text = _Company.ExportTurnover;

            }
        }
    }
    #endregion

    #region Send Contact
    #region Get Email Send To Admin
    private string EmailSend()
    {


        Criteria criuser = new Criteria();
        ADESSA.Entities.UserCollection _UserCollection = UserService.GetUserListByParam(UserColumns.UserId, OrderDirection.ASC, criuser.Criter);
        User _User = new User();
        if (_UserCollection.Count > 0)
        {
            _User = _UserCollection[0];
            return _User.Email.ToString();
        }
        else
            return RTISettings.EmailAdministrator;

    }
    #endregion
    #region Send Mail
    protected void SendMail(string email, string namecompany, string content)
    {
        Criteria _cri = new Criteria();
        _cri.add("[" + NewsletterColumns.Order + "]", FetConstant.EmailSendAboutForCompany, Condition.EQUAL, Condition.AND);
        NewsletterCollection _NewsletterCollection = NewsletterService.GetNewsletterListByParam("[" + Newsletter._Table + "].[" + NewsletterColumns.CreatedDate + "]", OrderDirection.ASC, _cri.Criter);
        if (_NewsletterCollection != null)
        {
            if (_NewsletterCollection.Count > 0)
            {

                string subject = _NewsletterCollection[0].Title.Replace("Email_", "").ToString();

                string mailContent = string.Format(_NewsletterCollection[0].Content, namecompany, content);
                if (!Utility.SendMailDisplay(email, subject, mailContent, FetConstant.DisplayNameEmail, EmailSend()))
                {
                    Alert.Show("Send email for company unsussecc");
                    return;
                }
                if (!Utility.SendMailDisplay(EmailSend(), subject, mailContent, FetConstant.DisplayNameEmail, email))
                {
                    Alert.Show("Send email for company unsussecc");
                    return;
                }
                johndoe.Value = string.Empty;
                Alert.Show("Send email for company sussecc");
            }
        }



    }

    #endregion

    #endregion
    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (Session["CompanyID"] != null)
        {
            this.Id = long.Parse(Session["CompanyID"].ToString());
            Company _Company = CompanyService.GetCompany(this.Id);
            if (_Company != null)
            {

                nameCompany = _Company.CompanyName;
                emailCompany = _Company.Email;
                SendMail(emailCompany, nameCompany, johndoe.Value);
            }
        }
    }
}
